package com.erpanderp.wam.adm.model.service;

import java.util.List;

import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.Session;

import com.erpanderp.wam.ac.model.entity.reimbursement.TravelExpense;
import com.erpanderp.wam.adm.model.entity.Note;
import com.erpanderp.wam.tm.model.entity.RecruitmentProfile;
import com.erpanderp.wam.util.HibernateUtil;

public class NoteDAO {

	public Note createNote(Note note) {
		try {
			Session session = HibernateUtil.getSessionFactory()
					.getCurrentSession();
			session.getTransaction().begin();
			session.setFlushMode(FlushMode.COMMIT);
			session.saveOrUpdate(note);
			session.getTransaction().commit();
			return note;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	
	//get all notes list
	 public List<Note> getAllNotesByStatus(String status) {
		List<Note> notes = null;
		try {
			Session session = HibernateUtil.getSessionFactory().getCurrentSession();
			session.beginTransaction();
			session.setFlushMode(FlushMode.COMMIT);				
			String queryString = "from Note n where n.status=:status ORDER BY n.displayOrder ASC";				
			Query query = session.createQuery(queryString);
			query.setCacheable(false).setString("status",status);				
			notes = (List<Note>) query.list();
			session.getTransaction().commit();
			return notes;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}
	 
	 public boolean udateNotes(List<Note> notesList){
		 System.out.println("DAO============="+notesList.size());
			boolean noteUpdated = false;
			try{
				Session session = HibernateUtil.getSessionFactory().getCurrentSession();
				session.beginTransaction();
				session.setFlushMode(FlushMode.AUTO);
			
				for(Note note : notesList){
					session.saveOrUpdate(note);
				}
				session.getTransaction().commit();
				noteUpdated = true;
			}catch(Exception e){
				e.printStackTrace();
				return noteUpdated;				
			}
			System.out.println("========noteUpdated==========="+noteUpdated);
			return noteUpdated;
		}	
}
